package com.ztesoft.report.controller;


import com.ztesoft.report.base.controller.BaseController;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.entity.ReportUpload;
import com.ztesoft.report.entity.TempUploadLog;
import com.ztesoft.report.service.ReportUploadService;
import com.ztesoft.report.service.TempUploadLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Map;

/**
 * <p>
 * 上传日志表 前端控制器
 * </p>
 *
 * @author 谭宏伟
 * @since 2018-09-18
 */
@RestController
@RequestMapping("/tempUpload")
public class TempUploadLogController extends BaseController {
    @Autowired
    TempUploadLogService tempUploadLogService;

    @Autowired
    ReportUploadService reportUploadService;

    @Value("${com.ztesoft.createExcelPath}")
    private String uploadPath;

    @RequestMapping("/list")
    public Object list(@RequestBody Map<String, Object> map) {
        return tempUploadLogService.queryPage(map);
    }

    /**
     * 删除操作，执行2步，首先删除服务器上面的EXCEL，然后在删除TempUpload_log表中的记录
     *
     * @param map
     * @return
     */
    @RequestMapping("/deleteTemp")
    public Object deleteTemp(@RequestBody Map<String, Object> map) {
        Integer serverId = Integer.parseInt(map.get("serverId").toString());
        ReportUpload reportUpload = reportUploadService.selectById(serverId);
        if (reportUpload == null) {
            return new R().error("未找到相关服务器的数据，无法执行删除");
        }
        String fileName = map.get("tempName").toString();
        //删除服务器上面的EXCEL
        try {
            tempUploadLogService.deleteFileOnServer(reportUpload, fileName);
            //删除表中记录
            return tempUploadLogService.deleteById(Integer.parseInt(map.get("id").toString()));
        } catch (Exception e) {
            return new R().error();
        }
    }

    /**
     * 上传文件由于前台同时传送表单数据会有BUG，所以分2步处理，这个方法是先接受到文件，然后copy到本地，
     * 然后把文件名传回前台
     * @param file
     * @return
     */
    @RequestMapping("/uploadExcel")
    @ResponseBody
    public Object uploadExcel(MultipartFile file) {
        //文件名
        String fileName = file.getOriginalFilename();
        try {
            Files.copy(file.getInputStream(), Paths.get(uploadPath, fileName), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(e.toString());
        }
        return R.ok(fileName);
    }

    /**
     * 这是上传文件第二步，获取表单数据的目标服务器ID 和 模板类型 以及文件名，
     * 然后通过SFTP传送到目标服务器的地址
     * @param map
     * @return
     */
    @RequestMapping("/insertTempUpload")
    public Object insertTempUpload(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        //获取上传服务器ID
        if (map.get("serverId") == null) {
            return R.error("未接收到上传地址服务器，请检查！");
        }
        Integer serverId = Integer.parseInt(map.get("serverId").toString());
        if (map.get("tempType") == null) {
            return R.error("未接收到模板类型，请检查！");
        }
        String tempType = map.get("tempType").toString();
        if (map.get("fileName") == null) {
            return R.error("未接收到文件名，请检查！");
        }
        String fileName = map.get("fileName").toString();
        //获取整个上传服务器实体类
        ReportUpload reportUpload = reportUploadService.selectById(serverId);
        //本地文件路径
        String localUrl = uploadPath + File.separator + fileName;
        //上传EXCEL
        tempUploadLogService.uploadExcel(reportUpload, localUrl);
        //上传成功后往TempUpload日志表里面加一个记录
        TempUploadLog tempUploadLog = new TempUploadLog();
        tempUploadLog.setTempName(fileName);
        tempUploadLog.setTempType(tempType);
        tempUploadLog.setServerId(reportUpload.getId());
        tempUploadLog.setUploadPath(reportUpload.getUploadPath());
        tempUploadLogService.insert(tempUploadLog);
        return R.ok();
    }

}
